<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html> 
<head> 
	<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
	<TITLE>한글 초성/중성/종성 숫자 세기</TITLE>
<SCRIPT>
var firstCharArr = Array(
'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ',
'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ',
'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' );

var middleCharArr = Array(
'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ',
'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ',
'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ',
'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ' );

var lastCharArr = Array(
'받침없음', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ',
'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ',
'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' );

function writeTable(arr, title) {
	document.write("<table id='"+title+"'><tr><td>"+title+"</td><td>갯수</td></tr>");
	for(i = 0; i < arr.length; i++) {
		document.write("<tr><td>"+arr[i]+"</td><td>0</td></tr>");
	}
	document.write("</table>");
}

function writeCount(sumArr, title) {
	var tbl = document.getElementById(title);
	for(var i = 0; i < sumArr.length; i++)
		tbl.rows[i+1].cells[1].innerText = sumArr[i];	
}

function countHangul() {

	var firstCharSum = new Array();
	for(i = 0; i < firstCharArr.length; i++)
		firstCharSum.push(0);

	var middleCharSum = new Array();
	for(i = 0; i < middleCharArr.length; i++)
		middleCharSum.push(0);
		
	var lastCharSum = new Array();
	for(i = 0; i < lastCharArr.length; i++)
		lastCharSum.push(0);

	var s = document.getElementById('teststring').value;
	for(j = 0; j < s.length; j++) {
		var unicode = s.charCodeAt(j) - 0xAC00;

		var lastChar = unicode % 28;
		if (lastChar >= 0 && lastChar < lastCharArr.length) 
			lastCharSum[lastChar]++;
		var middleChar = ( ( unicode - lastChar ) / 28 ) % 21;
		if (middleChar >= 0 && middleChar < middleCharArr.length) 
			middleCharSum[middleChar]++;
		var firstChar = parseInt (( ( unicode - lastChar ) / 28 ) / 21);
		if (firstChar >= 0 && firstChar < firstCharArr.length) 
			firstCharSum[firstChar]++;
	}
	writeCount(firstCharSum, "초성");
	writeCount(middleCharSum, "중성");
	writeCount(lastCharSum, "종성");

}

</SCRIPT>
</head> 
<body> 
<h1>문장 속의 한글 자모 갯수 세기</h1>
<h3>아래 입력박스에 한글 문장을 입력한 후 밑의 버튼을 누르면 오른쪽 표에
초성/중성/종성 각각의 갯수가 표시됩니다.<br/>
오른쪽 표를 긁어서 복사한 후 엑셀에 붙여넣기 하면 그대로 표로 만들어집니다.</h3>
<table>
<tr>
<td valign=top>
<textarea id=teststring cols=80 rows=30></textarea><br>
<input type=button value="한글자모갯수세기" onclick="countHangul();"/>
</td>
<td><script>
document.write("<table><tr><td valign=top>");
writeTable(firstCharArr, "초성");
document.write("</td><td valign=top>");
writeTable(middleCharArr, "중성");
document.write("</td><td valign=top>");
writeTable(lastCharArr, "종성");
document.write("</td><tr></table>");
</script>
</td></tr></table>
</body>
</html> 